package cn.bjxy.shishaolong.system.profession_course.controller;


import cn.bjxy.shishaolong.common.ResultMsg;
import cn.bjxy.shishaolong.common.StatusCode;
import cn.bjxy.shishaolong.common.SysConst;
import cn.bjxy.shishaolong.common.Utils;
import cn.bjxy.shishaolong.system.profession_course.entity.AssinCourse;
import cn.bjxy.shishaolong.system.profession_course.entity.Course;
import cn.bjxy.shishaolong.system.profession_course.entity.Profession;
import cn.bjxy.shishaolong.system.profession_course.entity.ProfessionCourseRelation;
import cn.bjxy.shishaolong.system.profession_course.service.ICourseService;
import cn.bjxy.shishaolong.system.profession_course.service.IProfessionCourseRelationService;
import cn.bjxy.shishaolong.system.profession_course.service.IProfessionService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 时少龙
 * @since 2019-03-12
 */
@CrossOrigin
@RestController
@RequestMapping("/api/admin/pc/profession")
public class ProfessionController {

    private static String ALL_COURSE_LIST = "allCourse";
    private static String ALL_PROFESSION_LIST = "allProfession";
    private static String ALL_RELATION_LIST = "relation";

    @Autowired
    private IProfessionService professionService;
    @Autowired
    private IProfessionCourseRelationService relationService;
    @Autowired
    private ICourseService courseService;

    /**
     * 条件查询
     *
     * @param param
     * @return
     */
    @RequestMapping("/queryByCondition")
    public ResultMsg queryByCondition(@RequestBody JSONObject param) {
        try {
            //查询条件
            Profession profession = null;
            if (param.containsKey(SysConst.QUERY_CONDITION_KEY)) {
                profession = param.getObject(SysConst.QUERY_CONDITION_KEY, Profession.class);
            }
            //分页查询
            if (param.containsKey(SysConst.PAGA_CURRENT_KEY) && param.containsKey(SysConst.PAGA_SIZE_KEY)) {
                IPage<Profession> page = new Page<>();
                page.setCurrent(param.getLong(SysConst.PAGA_CURRENT_KEY)).setSize(param.getLong(SysConst.PAGA_SIZE_KEY));
                IPage<Profession> list = professionService.queryByCondition(page, profession);
                return ResultMsg.success(list);
            } else {  //普通查询
                List<Profession> list = professionService.queryByCondition(profession);
                return ResultMsg.success(list);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.create(StatusCode.QUERY_FAILURE, StatusCode.QUERY_FAILURE_MSG);
        }
    }

    /**
     * 保存
     *
     * @param profession
     * @return
     */
    @PostMapping("/save")
    public ResultMsg save(@RequestBody Profession profession) {
        try {
            if (profession == null) {
                return ResultMsg.create(StatusCode.PARAMETER_ERROR, StatusCode.PARAMETER_ERROR_MSG);
            }
            // 给修改日期赋值
            profession.setUpdateAt(LocalDateTime.now());
            professionService.saveOrUpdate(profession);
            return ResultMsg.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.create(StatusCode.SAVE_FAILURE, StatusCode.SAVE_FAILURE_MSG);
        }
    }

    /**
     * 删除
     *
     * @param param
     * @return
     */
    @DeleteMapping("/delect")
    public ResultMsg delect(@RequestBody JSONObject param) {
        try {
            if (param == null || !param.containsKey(SysConst.BATCH_ID)
                    || !param.getString(SysConst.BATCH_ID).contains(SysConst.BATCH_ID_SEPARATOR)) {
                return ResultMsg.create(StatusCode.PARAMETER_ERROR, StatusCode.PARAMETER_ERROR_MSG);
            }
            String ids = param.getString(SysConst.BATCH_ID);
            List idlist = Utils.idsToList(ids, SysConst.BATCH_ID_SEPARATOR);
            professionService.removeByIds(idlist);
//            sysAdminService.remove(ids);
            return ResultMsg.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.create(StatusCode.DELETE_FAILUER, StatusCode.DELETE_FAILUER_MSG);
        }
    }

    /**
     * 获取分配课程的数据
     *
     * @param param
     * @return
     */
    @RequestMapping("/getAssinCourseData")
    public ResultMsg getAssinCourseData(@RequestBody JSONObject param) {
        try {
            String professionId = param.getString(SysConst.ID);
            if (StringUtils.isEmpty(professionId)) {
                return ResultMsg.create(StatusCode.PARAMETER_ERROR, StatusCode.PARAMETER_ERROR_MSG);
            }
            AssinCourse assinCourseData = professionService.getAssinCourseData(professionId);
            return ResultMsg.success(assinCourseData);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.create(StatusCode.QUERY_FAILURE, StatusCode.QUERY_FAILURE_MSG);
        }
    }

    /**
     * 保存分配课程的数据
     *
     * @param assinCourse
     * @return
     */
    @RequestMapping("/saveAssinCourseData")
    public ResultMsg saveAssinCourseData(@RequestBody AssinCourse assinCourse) {
        try {
            if (Objects.isNull(assinCourse)) {
                return ResultMsg.create(StatusCode.PARAMETER_ERROR, StatusCode.PARAMETER_ERROR_MSG);
            }
            // 保存分配的课程
            professionService.saveAsignCourse(assinCourse);
            return ResultMsg.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.create(StatusCode.SAVE_FAILURE, StatusCode.SAVE_FAILURE_MSG);
        }
    }

    /**
     * 获取专业和课程的对应关系
     *
     * @return 返回所有专业的数组
     */
    @RequestMapping("/gerProfessionCourseMap")
    public ResultMsg getProfessionCourseMap() {
        try {
            List<ProfessionCourseRelation> list = relationService.list();
            return ResultMsg.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.create(StatusCode.QUERY_FAILURE, StatusCode.QUERY_FAILURE_MSG);
        }
    }

    /**
     * 获取所有专业课程，课程专业对应关系  前端在用，不能加权限验证
     * @return
     */
    @RequestMapping("/getAllProfessionCourse")
    public ResultMsg getAllProfessionCourse(){
        try {
            List<Course> courseList = courseService.list();
            List<Profession> professionList = professionService.list();
            List<ProfessionCourseRelation> relationList = relationService.list();

            // 把查询的结果放到map集合
            HashMap<String, List> allProfessionCourse = new HashMap<>();
            allProfessionCourse.put(ALL_COURSE_LIST,courseList);
            allProfessionCourse.put(ALL_PROFESSION_LIST,professionList);
            allProfessionCourse.put(ALL_RELATION_LIST,relationList);
            return ResultMsg.success(allProfessionCourse);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.create(StatusCode.QUERY_FAILURE, StatusCode.QUERY_FAILURE_MSG);
        }
    }
}
